package com.mdd.admin.controller.competition;

import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.aop.Log;
import com.mdd.admin.service.ICompetitionService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.competition.*;
import com.mdd.admin.vo.competition.CompetitionDetailVo;
import com.mdd.admin.vo.competition.CompetitionListedVo;
import com.mdd.admin.vo.competition.CompetitionPlayVo;
import com.mdd.admin.vo.competition.CompetitionSessionListedVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.competition.CompetitionMatchDetail;
import com.mdd.common.entity.competition.CompetitionScore;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/adminapi/competition.list")
@Api(tags = "赛事列管理")
public class CompetitionController {

    @Resource
    ICompetitionService iCompetitionService;

    @GetMapping("/lists")
    @ApiOperation(value="赛事列列表")
    public AjaxResult<PageResult<CompetitionListedVo>> list(@Validated PageValidate pageValidate,
                                                            @Validated CompetitionSearchValidate searchValidate) {
        PageResult<CompetitionListedVo> list = iCompetitionService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="赛事列详情")
    public AjaxResult<CompetitionDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        CompetitionDetailVo detail = iCompetitionService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "赛事列新增")
    @PostMapping("/add")
    @ApiOperation(value="赛事列新增")
    public AjaxResult<Object> add(@Validated @RequestBody CompetitionCreateValidate createValidate) {
        // 获取当前的用户
        Integer adminId = LikeAdminThreadLocal.getAdminId();
        createValidate.setCreateId(adminId);
        iCompetitionService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "赛事列编辑")
    @PostMapping("/edit")
    @ApiOperation(value="赛事列编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody CompetitionUpdateValidate updateValidate) {
        iCompetitionService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "赛事列删除")
    @PostMapping("/del")
    @ApiOperation(value="赛事列删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iCompetitionService.del(idValidate.getId());
        return AjaxResult.success();
    }

    @Log(title = "赛事申请审核")
    @PostMapping("/examine")
    @ApiOperation(value="赛事申请审核")
    public AjaxResult<Object> examine(@Validated @RequestBody CompetitionApplyExamineValidate updateValidate) {
        iCompetitionService.examine(updateValidate);
        return AjaxResult.success("审核成功");
    }

    @Log(title = "赛事场次顺序")
    @GetMapping("/session")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult<CompetitionSessionListedVo> session(@Validated CompetitionSessionValidate sessionValidate) {
        CompetitionSessionListedVo session = iCompetitionService.session(sessionValidate);
        return AjaxResult.success(session);
    }

    @Log(title = "赛事场次顺序")
    @PostMapping("/session/up")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult sessionUp(@Validated @RequestBody CompetitionSessionDownValidate sessionValidate) {
        iCompetitionService.sessionUp(sessionValidate);
        return AjaxResult.success();
    }

    @Log(title = "赛事场次顺序")
    @PostMapping("/session/down")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult sessionDown(@Validated @RequestBody CompetitionSessionDownValidate sessionValidate) {
        iCompetitionService.sessionDown(sessionValidate);
        return AjaxResult.success();
    }

    @Log(title = "赛事场地")
    @GetMapping("/site")
    @ApiOperation(value="赛事申请审核")
    public AjaxResult<Object> site(@Validated CompetitionApplyExamineValidate updateValidate) {
        iCompetitionService.examine(updateValidate);
        return AjaxResult.success("审核成功");
    }

    @Log(title = "赛事场地")
    @PostMapping("/site/add")
    @ApiOperation(value="赛事申请审核")
    public AjaxResult<Object> siteAdd(@Validated @RequestBody CompetitionApplyExamineValidate updateValidate) {
        iCompetitionService.examine(updateValidate);
        return AjaxResult.success("审核成功");
    }

    @Log(title = "赛事场地")
    @PostMapping("/site/edit")
    @ApiOperation(value="赛事申请审核")
    public AjaxResult<Object> siteEdit(@Validated @RequestBody CompetitionApplyExamineValidate updateValidate) {
        iCompetitionService.examine(updateValidate);
        return AjaxResult.success("审核成功");
    }

    @Log(title = "赛事场地删除")
    @PostMapping("/site/del")
    @ApiOperation(value="赛事场地删除")
    public AjaxResult<Object> siteDel(@Validated @RequestBody IdValidate idValidate) {
        iCompetitionService.del(idValidate.getId());
        return AjaxResult.success();
    }


    @Log(title = "赛事场地")
    @GetMapping("/bisai")
    @ApiOperation(value="赛事申请审核")
    public AjaxResult<Object> bisai(@Validated CompetitionApplyExamineValidate updateValidate) throws Exception {
        Integer i = iCompetitionService.bisai(updateValidate);
        if(i==1){
            return AjaxResult.success("生成赛事晋级成功！");
        }else{
            return AjaxResult.success("生成赛事场次成功！");
        }
    }

    @GetMapping("/play/detail")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult playDetail(@RequestParam Integer matchId) {
        CompetitionMatchDetail play = iCompetitionService.playDetail(matchId);
        return AjaxResult.success(play);
    }

    @Log(title = "赛事场地")
    @PostMapping("/play/add")
    @ApiOperation(value="赛事申请审核")
    public AjaxResult<Object> playAdd(@Validated @RequestBody CompetitionMatchDetail updateValidate) {
        iCompetitionService.playAdd(updateValidate);
        return AjaxResult.success("审核成功");
    }

    @Log(title = "赛事场地")
    @PostMapping("/play/edit")
    @ApiOperation(value="赛事申请审核")
    public AjaxResult<Object> playEdit(@Validated @RequestBody CompetitionMatchDetail updateValidate) {
        iCompetitionService.playEdit(updateValidate);
        return AjaxResult.success("审核成功");
    }

    @GetMapping("/play")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult play(@RequestParam Integer id,@RequestParam Integer matchId) {
        CompetitionPlayVo play = iCompetitionService.play(id,matchId);
        return AjaxResult.success(play);
    }

    @Log(title = "提交成绩")
    @PostMapping("/play/score/edit")
    @ApiOperation(value="提交成绩")
    public AjaxResult<Object> playScoreEdit(@Validated @RequestBody CompetitionScore updateValidate) throws NoSuchFieldException, IllegalAccessException {
        iCompetitionService.playScoreEdit(updateValidate);
        return AjaxResult.success("提交成功");
    }
}
